import {
  ArgumentsHost,
  Catch,
  ExceptionFilter,
  Inject,
  LoggerService,
  UnprocessableEntityException
} from '@nestjs/common'
import { HttpAdapterHost } from '@nestjs/core'
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston'

import { MESSAGES } from '@/constants'

/**
 * 参数校验错误处理
 */
@Catch(UnprocessableEntityException)
export class ValidationFilter implements ExceptionFilter {
  constructor(
    @Inject(WINSTON_MODULE_NEST_PROVIDER)
    private readonly loggerService: LoggerService,
    private readonly httpAdapterHost: HttpAdapterHost
  ) {}

  catch(exception: UnprocessableEntityException, host: ArgumentsHost) {
    const { httpAdapter } = this.httpAdapterHost
    const ctx = host.switchToHttp()
    const status = exception.getStatus()
    const message = `${MESSAGES.UNPROCESSABLE_ENTITY}: ${exception.message}`

    this.loggerService.error(message, exception.stack, ValidationFilter.name)

    httpAdapter.reply(
      ctx.getResponse(),
      {
        status,
        message
      },
      status
    )
  }
}
